﻿<%@ WebHandler Language="C#" Class="WXActivity.PostHandler" %>

using System;
using System.Web;
using Activity;
using Activity.API;
using Activity.Data;

namespace WXActivity
{
    /// <summary>
    /// Post一般处理程序的测试
    /// </summary>
    public class PostHandler : BaseHandler
    {
        public PostHandler()
        {
            //修改请求为post 方式
            base._httpReuqest = base.Context.Request.Form;
        }

        /// <summary>
        /// GetAccessToken
        /// </summary>
        public void GetOpenID()
        {
            var code = _httpReuqest["code"];
            if (string.IsNullOrWhiteSpace(code))
            {
                Error("无效的[Code]值");
            }
            else
            {
                var result = WeixinApi.GetOpenID(code);
                if (result == null)
                    Error("未找到数据");
                else
                    Success(result);
            }
        }

        /// <summary>
        /// 检验用户是否注册
        /// </summary>
        public void CheckUser()
        {
            var openid = _httpReuqest["openid"];
            var result = UserPlayHandler.CheckUser(openid);
            Success(result);
        }

        /// <summary>
        /// 抽奖
        /// </summary>
        public void Lottery()
        {
            var openId = _httpReuqest["openId"];
            var lottery = new LotteryHandler();
            var result = lottery.DrawPrize(openId);
            Success(result);
        }

        /// <summary>
        /// 新年红包用户检测
        /// </summary>
        public void HappyNewYearCheckUser()
        {
            //把页访问次数写入redis缓存
            var redPac = new HappyNewYearHandler();
            redPac.PageBrowseNumber();
            var code = _httpReuqest["code"];
            if (string.IsNullOrWhiteSpace(code))
            {
                var returnError = new Messages { IsSuc = false, Code = -2, Message = "无效的[Code]值" };
                Success(returnError);
            }
            else
            {
                var result = WeixinApi.GetOpenID(code);
                if (result == null || result.openid == null)
                {
                    var returnError1 = new Messages { IsSuc = false, Code = -3, Message = "未找到openid" };
                    Success(returnError1);
                }
                else
                {
                    string openid = result.openid;
                    var result1 = redPac.CheckUser(openid);
                    Success(result1);
                }
            }
        }
        /// <summary>
        /// 新年领取红包
        /// </summary>
        public void HappyNewYear()
        {
            var openid = _httpReuqest["openId"];
            var redPac = new HappyNewYearHandler();
            var result = redPac.GetCollarRed(openid);
            Success(result);
        }
        /// <summary>
        /// 新年红包领取统计
        /// </summary>
        public void HappyNewYearCount()
        {
            var StartDate = _httpReuqest["StartDate"];
            var EndDate = _httpReuqest["EndDate"];
            var redPac = new HappyNewYearHandler();
            var result = redPac.HappyNewYearCount(StartDate, EndDate);
            Success(result);
        }
        /// <summary>
        /// 清空红包和浏览数redis缓存
        /// </summary>
        public void RemoverRedisKey()
        {
            var redPac = new HappyNewYearHandler();
            redPac.RemoverRedNum();
            Success(0);
        }

        /// <summary>
        /// 验证小票
        /// </summary>
        public void CheckNote()
        {
            var openid = _httpReuqest["openid"];
            var poscode = _httpReuqest["poscode"];
            var result = UserPlayHandler.CheckNote(openid, poscode);
            Success(result);
        }

        /// <summary>
        /// 个人中奖列表
        /// </summary>
        public void LotteryList()
        {
            var openId = _httpReuqest["openId"];
            var lottery = new LotteryHandler();
            var result = lottery.LotteryList(openId);
            Success(result);
        }

        public void Add()
        {
            var CardID = _httpReuqest["CardID"];
            var CardCode = _httpReuqest["CardCode"];
            var ShopID = _httpReuqest["ShopID"];
            var lottery = new LotteryHandler();
            lottery.CalculateIntegral(CardID, CardCode, ShopID);
            Success(0);
        }

        /// <summary>
        /// 添加求职
        /// </summary>
        public void CreateJob()
        {
            try
            {
                var UserName = _httpReuqest["UserName"];
                var Address = _httpReuqest["Address"];
                var Mobile = _httpReuqest["Mobile"];
                var Birthday = _httpReuqest["Birthday"];
                var Sex = _httpReuqest["Sex"];

                if (string.IsNullOrWhiteSpace(UserName))
                {
                    Error("【名称】不能为空");
                    return;
                }
                if (string.IsNullOrWhiteSpace(Mobile))
                {
                    Error("【手机】不能为空");
                    return;
                }
                if (string.IsNullOrWhiteSpace(Birthday))
                {
                    Error("【出生年月】不能为空");
                    return;
                }
                if (string.IsNullOrWhiteSpace(Sex))
                {
                    Error("【性别】不能为空");
                    return;
                }

                if (UserName.Length > 50)
                {
                    Error("【名称】超出限制长度");
                    return;
                }
                if (Mobile.Length > 13)
                {
                    Error("【手机】超出限制长度");
                    return;
                }
                if (!string.IsNullOrWhiteSpace(Address) && Address.Length > 200)
                {
                    Error("【备注】超出限制长度");
                    return;
                }

                var job = new JobUser
                {
                    UserName = UserName,
                    Sex = Convert.ToInt32(Sex),
                    Birthday = Convert.ToDateTime(Birthday),
                    Salary = _httpReuqest["Salary"],
                    JobType = _httpReuqest["JobType"],
                    WorkType = _httpReuqest["WorkType"],
                    Email = _httpReuqest["Email"],
                    Mobile = Mobile,
                    Province = _httpReuqest["Province"],
                    City = _httpReuqest["City"],
                    Area = _httpReuqest["Area"],
                    Address = Address
                };
                var result = KmJobHandler.CreateJob(job);
                if (result > 0)
                    Success(result);
                else
                    Error("数据保存出错");
            }
            catch (Exception ex)
            {
                Log.WriteLog("参数错误：" + ex.Message);
            }

        }

        /// <summary>
        /// BC用户检查
        /// </summary>
        public void CheckBcUser()
        {
            string openId = _httpReuqest["openId"];
            var reslut = BCOpenShopLotteryHandler.CheckUser(openId);
            Success(reslut);
        }

        /// <summary>
        /// BC新店开业抽奖
        /// </summary>
        public void BcOpenShopLottory()
        {
            string openId = _httpReuqest["openId"];
            var result = BCOpenShopLotteryHandler.Lottery(openId);
            Success(result);
        }

        /// <summary>
        /// 获取用户地址
        /// </summary>
        public void GetBcUserAddress()
        {
            string openId = _httpReuqest["openId"];
            var result = BCOpenShopLotteryHandler.GetAddress(openId);
            Success(result);
        }

        /// <summary>
        /// BC用户领奖地址
        /// </summary>
        public void SaveBcOrder()
        {
            var openId = _httpReuqest["openId"];
            var userName = _httpReuqest["userName"];
            var address = _httpReuqest["address"];
            var mobile = _httpReuqest["mobile"];

            if (string.IsNullOrWhiteSpace(openId))
            {
                Error("【openid】不能为空！");
                return;
            }
            if (string.IsNullOrWhiteSpace(userName))
            {
                Error("【姓名】不能为空！");
                return;
            }
            if (string.IsNullOrWhiteSpace(address))
            {
                Error("【地址】不能为空！");
                return;
            }
            if (string.IsNullOrWhiteSpace(mobile))
            {
                Error("【手机】不能为空！");
                return;
            }

            if (userName.Length > 20)
            {
                Error("【名称】超出限制长度");
                return;
            }
            if (mobile.Length > 11)
            {
                Error("【手机】超出限制长度");
                return;
            }
            if (address.Length > 150)
            {
                Error("【地址】超出限制长度");
                return;
            }

            var data = new Activity.Comm.AddressData
            {
                openId = openId,
                UserName = userName,
                Address = address,
                Mobile = mobile
            };
            var result = BCOpenShopLotteryHandler.SaveOrder(data);
            if (result != -1)
                Success(result);
            else
                Error("收货地址保存出错！");
        }

        /// <summary>
        /// 抽奖
        /// </summary>
        public void OpenShopLottery()
        {
            var openId = _httpReuqest["openId"];
            var lottery = new OpenShopLotteryHandler();
            var result = lottery.DrawPrize(openId);
            Success(result);
        }

        /// <summary>
        /// 通过授权获取用户信息，返回openid
        /// </summary>
        public void GetOauth2User()
        {
            var code = _httpReuqest["code"];
            if (string.IsNullOrWhiteSpace(code)) Error("无效的[Code]值");
            Log.WriteLog("当前用户code：" + code);
            //var result = WeixinApi.GetOauth2User(code);
            var result = WeixinApi.GetOpenID(code);
            if (string.IsNullOrWhiteSpace(result.openid))
                Error("未找到数据");
            else
                Success(result.openid);
        }

        /// <summary>
        /// 返回结果：-1未抽奖,0未中奖，1-4对应的奖品
        /// </summary>
        public void CheckOpenLottery()
        {
            var openId = _httpReuqest["openId"];
            var lottery = new OpenShopLotteryHandler();
            var result = lottery.GetLottery(openId);
            Success(result);
        }

        /// <summary>
        /// 兑奖
        /// </summary>
        public void CashPrize()
        {
            var openId = _httpReuqest["openId"];
            var lottery = new OpenShopLotteryHandler();
            var result = lottery.CashPrize(openId);
            if (result != -1)
                Success(result);
            else
                Error("兑奖出错！");
        }

        /// <summary>
        /// 检测用户，并返回用户昵称与积分
        /// </summary>
        public void CreditCheckUser()
        {
            //var code = _httpReuqest["code"];
            //if (string.IsNullOrWhiteSpace(code)) Error("无效的[Code]值");
            //var oauthAcc = WeixinApi.GetOpenID(code);
            //if(oauthAcc == null) Error("获取accessToken失败！");
            //var credit = new CreditExChangeHandler();
            //var Integral = credit.CheckUserInfo(oauthAcc.openid);
            //if (Integral == -1) Error("亲，您还没注册成为会员！");
            //var wxUser = WeixinApi.GetUserInfo(oauthAcc.openid, oauthAcc.access_token);
            //var result = new UserInfo()
            //{
            //    OpenId = oauthAcc.openid,
            //    NickName = wxUser.NickName,
            //    Integral = Integral
            //};
            //Success(result);
        }

        /// <summary>
        /// 积分兑换
        /// </summary>
        public void CreditExChange()
        {
            var openId = _httpReuqest["openId"];
            var Integral = Convert.ToInt32(_httpReuqest["Integral"]);
            var credit = new CreditExChangeHandler();
            var result = credit.ExChange(openId, Integral);
            Success(result);
        }

        /// <summary>
        /// 返回兑换列表
        /// </summary>
        public void CreditGetList()
        {
            var openId = _httpReuqest["openId"];
            if (string.IsNullOrWhiteSpace(openId))
            {
                Error("openId不能为空！");
                return;
            }
            var credit = new CreditExChangeHandler();
            var customerInfo = credit.CheckUserInfo(openId);
            if (customerInfo.Integral == -1)
            {
                Error("亲，您还没注册成为会员！");
                return;
            }
            var userInfo = WeixinApi.GetUserInfo(openId);
            if (userInfo == null)
            {
                Error("获取用户信息失败！");
                return;
            }
            ;
            var list = credit.GetList(openId);
            var result = new UserData
            {
                NickName = userInfo.NickName,
                Integral = customerInfo.Integral,
                List = list,
                THQNo = customerInfo.THQNo
            };
            Success(result);
        }

        /**************************KM顾客满意度调研****************************/

        /// <summary>
        /// 保存问卷结果
        /// </summary>
        public void Save()
        {
            var openId = _httpReuqest["openId"];
            var age = _httpReuqest["age"];
            var _age = int.Parse(age);
            var profession = _httpReuqest["profession"];
            var quality = _httpReuqest["quality"];
            var style = _httpReuqest["style"];
            var evaluate = _httpReuqest["evaluate"];
            var service = _httpReuqest["service"];
            var environment = _httpReuqest["environment"];
            var shopping = _httpReuqest["shopping"];
            var recommend = _httpReuqest["recommend"];
            var suggest = _httpReuqest["suggest"];
            if (string.IsNullOrWhiteSpace(openId))
                Error("【openid】不能为空！");
            if (string.IsNullOrWhiteSpace(age))
                Error("请选择年龄段！");
            if (string.IsNullOrWhiteSpace(profession))
                Error("【profession】不能为空！");
            if (string.IsNullOrWhiteSpace(quality))
                Error("【quality】不能为空！");
            if (string.IsNullOrWhiteSpace(style))
                Error("【style】不能为空！");
            if (string.IsNullOrWhiteSpace(evaluate))
                Error("【evaluate】不能为空！");
            if (string.IsNullOrWhiteSpace(service))
                Error("【service】不能为空！");
            if (string.IsNullOrWhiteSpace(environment))
                Error("【environment】不能为空！");
            if (string.IsNullOrWhiteSpace(shopping))
                Error("【shopping】不能为空！");
            if (string.IsNullOrWhiteSpace(recommend))
                Error("【recommend】不能为空！");
            var add = new wj_questionnaire()
            {
                openId = openId,
                age = _age,
                environment = environment,
                evaluate = evaluate,
                profession = profession,
                recommend = recommend,
                quality = quality,
                service = service,
                shopping = shopping,
                style = style,
                suggest = suggest
            };
            var quest = new QuestionnaireHandler();
            var message = quest.Save(add);
            if (message != null && message.IsSuc == false)
                Error(message.Message);
            else
                Success(message);
        }

        /// <summary>
        /// 获取问卷结果
        /// </summary>
        public void GetDetail()
        {
            var openId = _httpReuqest["openId"];
            if (string.IsNullOrWhiteSpace(openId))
                Error("openId不能为空！");
            var quest = new QuestionnaireHandler();
            var result = quest.GetDetail(openId);
            Success(result);
        }

        public void GetResult()
        {
            var quest = new QuestionnaireHandler();
            var result = quest.GetResult();
            Success(result);
        }

        /**************************KM顾客满意度调研****************************/

        public void SaveKM()
        {
            var code = _httpReuqest["JsonCode"];
            var km = new KMShopInvestigateHandler();
            var message = km.Save(code);
            if (message != null && message.IsSuc == false)
                Error(message.Message);
            else
                Success(message);
        }

        public void TotalKM()
        {
            var km = new KMShopInvestigateHandler();
            var result = km.Total();
            Success(result);
        }

        public void NewSaveKM()
        {
            var code = _httpReuqest["JsonCode"];
            var km = new NewKMShopInvestigateHandler();
            var message = km.Save(code);
            if (message != null && message.IsSuc == false)
                Error(message.Message);
            else
                Success(message);
        }

        public void NewTotalKM()
        {
            var km = new NewKMShopInvestigateHandler();
            var result = km.Total();
            Success(result);
        }

        public void TimeTrackingReport()
        {
            var handler = new TimeTrackingReportHandler();
            var result = handler.GetReport();
            if (result == null)
            {
                Error("获取数据异常");
            }
            else
            {
                Success(result);
            }
        }

        public void TimeAnalysisReport()
        {
            var EndDateStr1 = _httpReuqest["EndDate1"];
            var EndDateStr2 = _httpReuqest["EndDate2"];
            var EndDateStr3 = _httpReuqest["EndDate3"];
            var ShopTypeStr = _httpReuqest["ShopType"];
            if (string.IsNullOrWhiteSpace(EndDateStr1) || string.IsNullOrWhiteSpace(EndDateStr2) || string.IsNullOrWhiteSpace(EndDateStr3))
                Error("时间不能为空！");
            var EndDate1 = Convert.ToDateTime(EndDateStr1);
            var EndDate2 = Convert.ToDateTime(EndDateStr2);
            var EndDate3 = Convert.ToDateTime(EndDateStr3);
            var ShopType = Convert.ToInt32(ShopTypeStr);
            EndDate2 = EndDate2.AddHours(EndDate1.Hour);
            EndDate3 = EndDate3.AddHours(EndDate1.Hour);
            if (EndDate1.ToString("HH:00:00") != EndDate2.ToString("HH:00:00") || EndDate1.ToString("HH:00:00") != EndDate3.ToString("HH:00:00"))
            {
                Error("时间点不一样！");
            }
            else
            {
                var handler = new TimeAnalysisReportHandler();
                var result = handler.GetReprot(EndDate1, EndDate2, EndDate3, ShopType);
                if (result == null)
                {
                    Error("获取数据异常");
                }
                else
                {
                    Success(result);
                }
            }
        }

        public void TimeAnalysisReport2()
        {
            var EndDateStr1 = _httpReuqest["EndDate1"];
            var EndDateStr2 = _httpReuqest["EndDate2"];
            var EndDateStr3 = _httpReuqest["EndDate3"];
            var ShopTypeStr = _httpReuqest["ShopType"];
            if (string.IsNullOrWhiteSpace(EndDateStr1) || string.IsNullOrWhiteSpace(EndDateStr2) || string.IsNullOrWhiteSpace(EndDateStr3))
                Error("时间不能为空！");
            var EndDate1 = Convert.ToDateTime(EndDateStr1);
            var EndDate2 = Convert.ToDateTime(EndDateStr2);
            var EndDate3 = Convert.ToDateTime(EndDateStr3);
            var ShopType = Convert.ToInt32(ShopTypeStr);
            EndDate2 = EndDate2.AddHours(EndDate1.Hour);
            EndDate3 = EndDate3.AddHours(EndDate1.Hour);
            if (EndDate1.ToString("HH:00:00") != EndDate2.ToString("HH:00:00") || EndDate1.ToString("HH:00:00") != EndDate3.ToString("HH:00:00"))
            {
                Error("时间点不一样！");
            }
            else
            {
                var handler = new TimeAnalysisReportHandler();
                var result = handler.GetReprot2(EndDate1, EndDate2, EndDate3, ShopType);
                if (result == null)
                {
                    Error("获取数据异常");
                }
                else
                {
                    Success(result);
                }
            }
        }

        public void TimeTrackingSalePartReport()
        {
            var UserID = _httpReuqest["UserID"];
            var handler = new TimeTrackingSalePartReportHandler();
            var result = handler.FinalGet(UserID);
            if (result == null)
            {
                Error("获取数据异常");
            }
            else
            {
                Success(result);
            }
        }

        public void KMSalesYearOnYearReport()
        {
            var EndDateStr1 = _httpReuqest["EndDate1"];
            var EndDateStr2 = _httpReuqest["EndDate2"];
            var EndDateStr3 = _httpReuqest["EndDate3"];
            var LevelStr = _httpReuqest["Level"];
            if (string.IsNullOrWhiteSpace(EndDateStr1) || string.IsNullOrWhiteSpace(EndDateStr2) ||
                string.IsNullOrWhiteSpace(EndDateStr3))
                Error("时间不能为空！");
            else
            {
                if (string.IsNullOrEmpty(LevelStr))
                {
                    Error("请选择分部或者店铺！");
                }
                else
                {
                    var EndDate1 = Convert.ToDateTime(EndDateStr1);
                    var EndDate2 = Convert.ToDateTime(EndDateStr2);
                    var EndDate3 = Convert.ToDateTime(EndDateStr3);
                    EndDate2 = EndDate2.AddHours(EndDate1.Hour);
                    EndDate3 = EndDate3.AddHours(EndDate1.Hour);
                    if (EndDate1.ToString("HH:00:00") != EndDate2.ToString("HH:00:00") || EndDate1.ToString("HH:00:00") != EndDate3.ToString("HH:00:00"))
                    {
                        Error("时间点不一样！");
                    }
                    else
                    {
                        var Level = Convert.ToInt32(LevelStr);
                        var handler = new KMSalesYearOnYearReportHandler();
                        var result = handler.GetReport(EndDate1, EndDate2, EndDate3,Level);
                        if (result == null)
                        {
                            Error("获取数据异常");
                        }
                        else
                        {
                            Success(result);
                        }
                    }
                }
            }
        }
    }
}